package com.qingmi.farmer.ui.notice;


import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.model.Notice;
import com.qingmi.farmer.data.repository.data.OtherDataRepository;
import com.qingmi.farmer.data.request.GetNoticesReq;
import com.qingmi.farmer.databinding.ActivityNoticeBinding;
import com.wjjun.base.ui.base.ListActivity;
import com.qingmi.farmer.ui.webview.WebViewActivity;
import com.qingmi.farmer.viewmodel.NoticeItemViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 公告
 */
public class NoticeActivity extends ListActivity<NoticeViewModel, ActivityNoticeBinding> {

    public static void start(Context context) {
        Intent intent = new Intent(context, NoticeActivity.class);
        context.startActivity(intent);
    }

    @Inject
    OtherDataRepository otherDataRepository;


    @Override
    public int getLayoutId() {
        return R.layout.activity_notice;
    }

    @Override
    protected NoticeViewModel initViewModel() {
        return new NoticeViewModel();
    }

    @Override
    protected void initContent() {
        otherDataRepository.bind(this);
        super.initContent();
        viewModel.setTitle(getString(R.string.title_notice));
        commListViewModel.showDivider();
    }


    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        itemBinding.set(BR.viewModel, R.layout.layout_item_notice).bindExtra(BR.presenter, viewGroupPresenter);
    }

    @Override
    protected void onListItemClick(View view, BaseViewModel viewModel) {
        if (viewModel instanceof NoticeItemViewModel) {
            WebViewActivity.start(NoticeActivity.this, ((NoticeItemViewModel) viewModel).url, ((NoticeItemViewModel) viewModel).notice.get());
        }
    }


    @Override
    public void refreshData(boolean init) {
        super.refreshData(init);
        GetNoticesReq req = new GetNoticesReq();
        otherDataRepository.getNotices(new DefaultObserver<List<Notice>>() {

            @Override
            public void onNext(List<Notice> notice) {
                List<NoticeItemViewModel> list = new ArrayList<>();
                if (notice != null && !notice.isEmpty()) {
                    for (Notice item : notice) {
                        list.add(new NoticeItemViewModel(item));
                    }
                }
                resetData(list);
            }

            @Override
            public void onError(MyException e) {
                finishLoadMoreError();
            }
        }, req);

    }

    @Override
    public void loadData() {
        super.loadData();
        GetNoticesReq req = new GetNoticesReq();
        req.setNoticeId(((NoticeItemViewModel) commListViewModel.getLastItem()).id);
        otherDataRepository.getNotices(new DefaultObserver<List<Notice>>() {

            @Override
            public void onNext(List<Notice> notice) {
                List<NoticeItemViewModel> list = new ArrayList<>();
                if (notice != null && !notice.isEmpty()) {
                    for (Notice item : notice) {
                        list.add(new NoticeItemViewModel(item));
                    }
                }
                addData(list);
            }

            @Override
            public void onError(MyException e) {
                finishLoadMoreError();
            }

        }, req);
    }
}
